package com.xhwl.centiotdata.handler.visitor.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xhwl.centiotdata.handler.visitor.VisitorIntegrationFactory;
import com.xhwl.centiotdata.handler.visitor.VisitorIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.keytoplocal.VisitorInfoKeyTopLocalDTO;
import com.xhwl.centiotdata.pojo.dto.visitor.VisitorIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IAuthAccountCentService;
import com.xhwl.centiotdata.service.cent.IVisitorCentService;
import com.xhwl.centiotdata.suppliersystem.keytopLocal.VisitorKeyTopLocalHandler;
import com.xhwl.centiotdata.utils.Base64Utils;
import com.xhwl.common.dto.cent.visitor.VisitorAuthDTO;
import com.xhwl.common.dto.cent.visitor.VisitorInfoDTO;
import com.xhwl.common.dto.cent.visitor.VisitorOpenDTO;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.auth.AuthAccountMapping;
import com.xhwl.common.pojo.cent.visitor.Follower;
import com.xhwl.common.utils.ChkUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author msy
 */
@Service
@Slf4j
public class VisitorIntegrationKeyTopLocalHandler extends VisitorIntegrationHandler {

    @Autowired
    private VisitorKeyTopLocalHandler visitorKeyTopLocalHandler;

    @Autowired
    private IAuthAccountCentService authAccountCentService;

    @Autowired
    private IVisitorCentService iVisitorCentService;


    @Override
    public void afterPropertiesSet() throws Exception {
        VisitorIntegrationFactory.register(ProductEnum.KEYTOP_LOCAL.getId(), this);
    }

    /**
     * 预约授权
     * @param configDTO
     * @param authDTO
     * @return
     */
    @Override
    public ResultJson createVisitorAuth(VisitorIntegrationConfigDTO configDTO, VisitorAuthDTO authDTO) {
        log.info("科拓访客创建预约授权：configDTO:{}, authDTO:{}",
                JSONUtil.toJsonStr(configDTO), JSONUtil.toJsonStr(authDTO));

        AuthAccountMapping accountMapping = authAccountCentService.query(configDTO.getProjectId(), authDTO.getIntervieweeId(), configDTO.getProductId());
        if(accountMapping != null) {
            List<VisitorInfoKeyTopLocalDTO.Visitor> visitorList = new ArrayList<>();
            // 访客
            VisitorInfoKeyTopLocalDTO.Visitor visitor = new VisitorInfoKeyTopLocalDTO.Visitor();
            visitor.setVisitorTel(authDTO.getPhone());
            visitor.setVisitorName(authDTO.getName());
            if(ChkUtil.isNotNull(authDTO.getFaceUrl())) {
                visitor.setVisitorFace(Base64Utils.getBaseByUrl(authDTO.getFaceUrl()));
            }
            if(ChkUtil.isNotNull(authDTO.getCarNum())) {
                visitor.setVisitorPlateNo(authDTO.getCarNum());
            }
            visitorList.add(visitor);

            // 随行人
            List<Follower> followerList = authDTO.getFollowerList();
            if(ChkUtil.isNotNull(followerList)) {
                for(Follower follower : followerList) {
                    VisitorInfoKeyTopLocalDTO.Visitor v = new VisitorInfoKeyTopLocalDTO.Visitor();
                    v.setVisitorTel(follower.getPhone());
                    v.setVisitorName(follower.getName());
                    v.setVisitorFace(Base64Utils.getBaseByUrl(follower.getFaceUrl()));
                    visitorList.add(v);
                }
            }

            // 查询区域
            JSONArray areaList = visitorKeyTopLocalHandler.queryArea(configDTO, "");
            JSONObject jsonObject = new JSONObject(areaList.get(0));
            String areaId = jsonObject.getStr("id");

            VisitorInfoKeyTopLocalDTO visitorInfoKeyTopLocalDTO = new VisitorInfoKeyTopLocalDTO();
            visitorInfoKeyTopLocalDTO.setRespondentId(accountMapping.getOpenId());
            visitorInfoKeyTopLocalDTO.setRespondentName(authDTO.getIntervieweeName());
            visitorInfoKeyTopLocalDTO.setRespondentTel(authDTO.getIntervieweePhone());
            visitorInfoKeyTopLocalDTO.setVisitReason(authDTO.getSubjectMatter());
            visitorInfoKeyTopLocalDTO.setVisitTime(DateUtil.formatDateTime(authDTO.getAuthStartTime()));
            visitorInfoKeyTopLocalDTO.setDepartureTime(DateUtil.formatDateTime(authDTO.getAuthEndTime()));
            visitorInfoKeyTopLocalDTO.setVisitorConfigId(areaId);
            visitorInfoKeyTopLocalDTO.setVisitorInfoList(visitorList);

            VisitorOpenDTO visitorOpenDTO = visitorKeyTopLocalHandler.createVisitorAuth(configDTO, visitorInfoKeyTopLocalDTO, authDTO);
            iVisitorCentService.updateOpenIdById(visitorOpenDTO);
            return ResultJson.success();
        } else {
            return ResultJson.fail("受访者信息不存在");
        }
    }


    @Override
    public ResultJson removeVisitorAuth(VisitorIntegrationConfigDTO configDTO, VisitorInfoDTO infoDTO) {
        AuthAccountMapping accountMapping = authAccountCentService.query(configDTO.getProjectId(), infoDTO.getIntervieweeId(), configDTO.getProductId());
        if(accountMapping != null) {
            return visitorKeyTopLocalHandler.deleteVisitorAuth(configDTO, infoDTO, accountMapping.getOpenId());
        } else {
            return ResultJson.fail("科拓业主取消邀请访客：被访人不存在，无法取消访客邀请");
        }
    }


    @Override
    public ResultJson getQrCode(VisitorIntegrationConfigDTO configDTO, VisitorAuthDTO visitorAuthDTO) {
        return visitorKeyTopLocalHandler.getQrCode(configDTO, visitorAuthDTO);
    }

}
